<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单ID" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单ID"></el-input>
    </el-form-item>
    <el-form-item label="账单封面图" prop="iconUrl">
      <el-input v-model="dataForm.iconUrl" placeholder="账单封面图"></el-input>
    </el-form-item>
    <el-form-item label="账单标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="账单标题"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="金额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          billId: 0,
          orderId: '',
          iconUrl: '',
          title: '',
          createTime: '',
          amount: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单ID不能为空', trigger: 'blur' }
          ],
          iconUrl: [
            { required: true, message: '账单封面图不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '账单标题不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.billId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.billId) {
            this.$http({
              url: this.$http.adornUrl(`/payment/bill/info/${this.dataForm.billId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.bill.orderId
                this.dataForm.iconUrl = data.bill.iconUrl
                this.dataForm.title = data.bill.title
                this.dataForm.createTime = data.bill.createTime
                this.dataForm.amount = data.bill.amount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/payment/bill/${!this.dataForm.billId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'billId': this.dataForm.billId || undefined,
                'orderId': this.dataForm.orderId,
                'iconUrl': this.dataForm.iconUrl,
                'title': this.dataForm.title,
                'createTime': this.dataForm.createTime,
                'amount': this.dataForm.amount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
